/*
 * int_module.c
 *
 *	Project 6
 *  Created on: Apr 6, 2013
 *      Engineers: George Tkachuk & Brentton Garber 
 *       Emails: 	gtkachuk@asu.edu btgarber@asu.edu
 *      CSE 325: Embedded microprocessor Systems
 *      Spring 2013
 *      Description:	Configures 4 IRS's; Two PIT IRS's (one for refresh rate and the next step timing delay, one for calling reads from the nunchuk)
 *      									Two GPT IRS's (one for reset button and one for adding ghosts to the game)
 *      				
 */
#include "common.h"

// get the vector ram
extern uint32 __VECTOR_RAM[];
int screen_count;
//int program_mode;
// Initialize the handler for refreshing the LED Matrix
void set_pit_handler(int vector, void (*handler)(void))
{
	// Set the Interrupt handler function
	__VECTOR_RAM[vector]=(uint32)handler;
	
	// Unmask the 55th slot
	MCF_INTC0_IMRH &= ~(1) << 23;
	//set PIT's (55) interrupt level to 5 and priority to 7
	MCF_INTC0_ICR55 |= (5) << 3;
	MCF_INTC0_ICR55 |= (7);
}

// This PIT timer is responsible for both refreshing the LED matrix screen and refreshing the game unit positions
__declspec(interrupt) void pit_handler(void)
{
	// Clear the interrupt flag
	MCF_PIT0_PCSR |= 1 << 2;
	
	screen_count++;
	// Refresh the LED Matrix
	ledmatrix_refresh();
	if(screen_count == 256)
		{
			screen_count = 0;
			if(program_mode != LOST)
			{
				game_refresh();
			}
			else(player_dead_sound());
		}
}
// Initialize the handler for updating the game sequence
void set_pit_handler_1(int vector, void (*handler)(void))
{
	// Set the Interrupt handler function
	__VECTOR_RAM[vector]=(uint32)handler;
	
	// Unmask the 56th slot
	MCF_INTC0_IMRH &= ~(1) << 24;
	//set PIT's (56) interrupt level to 5 and priority to 7
	MCF_INTC0_ICR56 |= (4) << 3;
	MCF_INTC0_ICR56 |= (2);
}

// Interrupt handler for updating the game sequence
__declspec(interrupt) void pit_handler_1(void)
{
	// Clear the interrupt flag
	MCF_PIT1_PCSR |= 1 << 2;

	// Set the next game sequence to the LED Matrix
	play_note(-1, 0, 0);
	nunchuk_read();
}

// Initialize the handler for stopping the game
void set_gpt_handler(int vector, void (*handler)(void))
{
	// Set the Interrupt handler function
	__VECTOR_RAM[vector]=(uint32)handler;
	
	MCF_INTC0_IMRH &= ~(1) << 12;
	MCF_INTC0_ICR44 |= (5) << 3;
	MCF_INTC0_ICR44 |= (6);
}
// Interrupt handler to start the game
__declspec(interrupt) void gpt_handler(void)
{
	// Clear the interrupt flag
	MCF_GPT_GPTFLG1 |= 1;
	
	// add a ghost to the game board
	game_modGhost();
}

// Initialize the handler for stopping the game
void set_gpt_handler_1(int vector, void (*handler)(void))
{
	// Set the Interrupt handler function
	__VECTOR_RAM[vector]=(uint32)handler;
	
	MCF_INTC0_IMRH &= ~(1) << 13;
	MCF_INTC0_ICR45 |= (5) << 3;
	MCF_INTC0_ICR45 |= (6);
}

// Interrupt handler to stop the game
__declspec(interrupt) void gpt_handler_1(void)
{
	
	// Clear the interrupt flag
	MCF_GPT_GPTFLG1 |= 7;
	
	// Stop the game
	game_reset();
}

